


#from functions import formatinput, pMatch

import pickle, time
from random import randint, choice
import jplur
from jplur.entity import *
from jplur.objects import *


ESC_CLEAR_LINE        = "\x1b[2K"
ESC_CLEAR_SCREEN      = "\x1b[2J"
ESC_HOME_CURSOR       = "\x1b[1;1H"
ESC_RESET_TERMINAL    = "\x1bc"
ESC_REVERSE_VIDEO_ON  = "\x1b[7m"
ESC_REVERSE_VIDEO_OFF = "\x1b[27m"
ESC_CURSOR_DOWN       = "\x1b[B"
ESC_SCROLL_UP         = "\x1bM"
ESC_SCROLL_DOWN       = "\x1bD"
ESC_ERASE_LINE        = "\x1b[K"
ESC_ERASE_DOWN        = "\x1b[J"    # WARNING: Does not respect scrolling regions.
ESC_ERASE_UP          = "\x1b[1J"   # WARNING: Does not respect scrolling regions.
ESC_GFX_BLUE_FG       = "\x1b[34m"
ESC_GFX_OFF           = "\x1b[0m"


global map

class Player(Human):
	
	def __init__(self, channel):
		Human.__init__(self)
		
		self.channel = channel
		x = 128+randint(0,20)-10
		y = 128+randint(0,20)-10
		self.location = [6,128,128]
		
		
		
		
		#persistant
		
		self.logged = 0
		self.password = ""

		self.type = "players"

		
		self.desc = ["This is another player."]
		print "player init"
		
		self.symbol = "@"
		self.color = "ffffff"
		
		
		
		self.role = "player"
		self.update = time.time()
		self.oldhp = [0,0]
		self.oldexp = [0,0]
		self.speed = .4
		
		self.exp = 0
		
		jplur.players.append(self)
		
		self.running = 0
		self.walk = 0
		self.digging = 0
		
		self.heal = 0
		
		self.exp_table = [30, 120, 300, 750, 930, 1156, 1400, 1650, 2800, 5000, 10000, 10000, 10000, 10000, 10000]
		self.last_level = 0
		
		

##############################################
#           Internal Functions               #
##############################################

	def save(self): 
		#need to clear internal pointers to higher objects before pickle dump of player, then restore
		tmpC = self.channel
		tmpI = self.update
		
		
		self.channel = 0
		self.time = 0
		self.update = 0
		self.key = 0
		
		savefile = open('./players/'+self.name+'.sav', 'w')
		pickle.dump(self, savefile)
		savefile.close()
		
		self.channel = tmpC
		self.update = tmpI
		
	
	def send(self, data):
		self.channel.Send(data)
	
	
	
	def view(self, chunks):
		
		self.send( {"action":"fullview", "data":chunks } )
		
	def start(self):
		jplur.players.append(self)
		jplur.map.entity_hash.insert(self)
		self.send( {"action":"sync_maps", "data":jplur.map.sync_maps() } )
		self.send( {"action":"p", "data":self.location } ) #player location
		print "Player ",self.name, " starting!"
		#self.view() #send map chunks to the server
		self.move([0,0,0]) #testing
		self.send( {"action":"hp", "data":[self.hp,self.maxhp] } ) 
		
	def delete(self):
		print "player.delete()"
		if self.key != 0:
			last = jplur.map.entity_hash.query_key(self.key)
			if self in last:
				last.remove(self)
				print "removing hash pointer"
			self.save()
		if self in jplur.players:
			jplur.players.remove(self)
			print "removing jplur.players pointer"
		self.send({"action": "quit"})
		
	def movekey(self,data):
		# REMEMBER z y x
		directions = { 	55:[0,-1,-1],
						56:[0,-1,0],
						57:[0,-1,1],
						52:[0,0,-1],
						54:[0,0,1],
						49:[0,1,-1],
						50:[0,1,0],
						51:[0,1,1],
						60:[-1,0,0],
						62:[1,0,0]
		}
		
		
		self.walk = directions[data] 
		
	def run(self,data):
		# REMEMBER z y x
		directions = { 	55:[0,-1,-1],
						56:[0,-1,0],
						57:[0,-1,1],
						52:[0,0,-1],
						54:[0,0,1],
						49:[0,1,-1],
						50:[0,1,0],
						51:[0,1,1],
						60:[-1,0,0],
						62:[1,0,0]
		}
		
		if data in directions:
			self.running = directions[data] 
		
	def dig(self,data):
		if data == 0:
			self.digging = 0
		# REMEMBER z y x
		directions = { 	55:[0,-1,-1],
						56:[0,-1,0],
						57:[0,-1,1],
						52:[0,0,-1],
						54:[0,0,1],
						49:[0,1,-1],
						50:[0,1,0],
						51:[0,1,1],
						60:[-1,0,0],
						62:[1,0,0]
		}
		
		if data in directions:
			data = directions[data]
			tile = [ self.location[0]+data[0], self.location[1]+data[1], self.location[2]+data[2] ]
			if jplur.map.tile_at( tile ) != 0:
				self.digging = tile
		print self.digging
	
	
	def exp_check(self):
		if len(self.exp_table) >0:
			if self.exp > self.last_level + self.exp_table[0]:
				self.last_level = self.exp
				self.exp_table.pop(0)
				self.level += 1
				
				self.send({"action":"message", "data":"#:12ff00You've reached level "+str(self.level)+"!"})
				self.hpmax = int(self.hpmax + self.con/2)
				
				if self.level in [5,10,15]:
					self.attack_die[0] += 1
				
				if self.level in [2,4,6,8,10,12,14]:
					self.attack_die[1] += 1
					
				self.speed = self.speed * .9
				
				if self.char_class == "green":
					self.speed -= .01
					
				if self.char_class == "blue":
					if self.level in [2,5,8,10,16]:
						self.str += 1
						self.con += 1
				
				if self.char_class == "orange":
					self.exp = self.exp * 1.1
					
				if self.char_class == "purple":
					self.speed -= .005
					if self.level in [5,7,12,17,24]:
						self.attack_die[0] += 1
	
	
	def pump(self):
		
		self.exp_check()

		if self.oldhp != [self.hp, self.maxhp]:
			self.oldhp = [self.hp, self.maxhp]
			self.send( {"action":"hp", "data":[self.hp,self.maxhp] } ) 
		
		if self.oldexp != [self.exp, self.last_level+self.exp_table[0]]:
			self.oldexp = [self.exp, self.last_level+self.exp_table[0]]
			self.send( {"action":"exp", "data":[self.exp, self.last_level+self.exp_table[0]] } ) 
		
		if time.time() - self.update > self.speed:
			self.update = time.time()
			self.heal += 1
			if self.heal > 13:
				self.heal = 0
				am = int(self.maxhp/15)
				if am < 1:
					am = 1
				self.hp += am
				if self.hp > self.maxhp:
					self.hp = self.maxhp
			if self.walk != 0:
				self.running = 0
				self.move(self.walk)

				self.walk = 0
			elif self.running != 0:
				self.move(self.running)
				
			elif self.digging != 0:
				tile = self.digging
				if tile[0] <> self.location[0]:
					if tile[0] < self.location[0]:
						jplur.map.map[ self.location[0]][ tile[1]][ tile[2] ] = -5
						jplur.server.SendToAll({"action":"single","loc":[tile[0]+1, tile[1], tile[2]],"tile":-5})
						jplur.map.map[ tile[0]][ tile[1]][ tile[2] ] = -4
						jplur.server.SendToAll({"action":"single","loc":tile,"tile":-4})
					else:
						jplur.map.map[ self.location[0]][ tile[1]][ tile[2] ] = -4
						jplur.server.SendToAll({"action":"single","loc":[tile[0]-1, tile[1], tile[2]],"tile":-4})
						jplur.map.map[ tile[0]][ tile[1]][ tile[2] ] = -5
						jplur.server.SendToAll({"action":"single","loc":tile,"tile":-5})
				else:
					jplur.map.map[ tile[0]][ tile[1]][ tile[2] ] = -3
					jplur.server.SendToAll({"action":"single","loc":tile,"tile":-3})
				self.digging = 0
			#are we standing next to a target?
			found = self.get_adjacent_role("monster")

			if found:
				hit = self.attack(found)
				if hit:
					self.send({"action":"message","data":"#:dd2929"+"You hit "+found.name+" for "+str(hit)+"!"})
					found.hp -= hit
					if found.hp <= 0:
						found.die(self)
				else:
					self.send({"action":"message","data":"#:38e1df"+"You miss"+found.name+"!"})
		
	
